
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.dto.DistributionUserIncomeDTO;
import com.jf.cloud.distribution.model.DistributionUserIncome;
import com.jf.cloud.distribution.service.DistributionUserIncomeService;
import com.jf.cloud.distribution.vo.DistributionUserIncomeVO;
import com.jf.cloud.distribution.vo.DistributionUserVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 分销收入记录信息
 *
 * @author zz
 * @date 2021-08-09 14:14:09
 */
@RestController("platformDistributionUserIncomeController")
@RequestMapping("/p/distribution_user_income")
@Tag(name = "分销收入记录信息")
public class DistributionUserIncomeController {

    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销收入记录信息列表" , description = "分页获取分销收入记录信息列表")
    public ServerResponseEntity<PageVO<DistributionUserIncome>> page(@Valid PageDTO pageDTO) {
        PageVO<DistributionUserIncome> distributionUserIncomePage = distributionUserIncomeService.page(pageDTO);
        return ServerResponseEntity.success(distributionUserIncomePage);
    }

    @GetMapping("/effect_page")
    @Operation(summary = "获取分销推广效果列表" , description = "分页获取分销推广效果列表")
    public ServerResponseEntity<PageVO<DistributionUserIncomeVO>> effectPage(@Valid PageDTO pageDTO, @ParameterObject DistributionUserIncomeDTO distributionUserIncomeDTO, String userMobile) {
        PageVO<DistributionUserIncomeVO> distributionUserIncomePage = distributionUserIncomeService.effectPage(pageDTO, distributionUserIncomeDTO, userMobile, Constant.PLATFORM_SHOP_ID);
        if (BooleanUtil.isFalse(permission)) {
            for (DistributionUserIncomeVO record : distributionUserIncomePage.getList()) {
                DistributionUserVO distributionUser = record.getDistributionUser();
                if (Objects.nonNull(distributionUser)) {
                    if (StrUtil.isNotBlank(distributionUser.getUserMobile())) {
                        distributionUser.setUserMobile(PhoneUtil.hideBetween(distributionUser.getUserMobile()).toString());
                    }
                    if (StrUtil.isNotBlank(distributionUser.getNickName()) && PrincipalUtil.isMobile(distributionUser.getNickName())) {
                        distributionUser.setNickName(PhoneUtil.hideBetween(distributionUser.getNickName()).toString());
                    }
                }
            }
        }
        return ServerResponseEntity.success(distributionUserIncomePage);
    }

    @GetMapping
    @Operation(summary = "获取分销收入记录信息" , description = "根据incomeId获取分销收入记录信息")
    public ServerResponseEntity<DistributionUserIncome> getByIncomeId(@RequestParam Long incomeId) {
        return ServerResponseEntity.success(distributionUserIncomeService.getByIncomeId(incomeId));
    }


}
